package com.jajahome.feature.user.adapter;

import android.content.Context;
import android.content.Intent;
import androidx.viewpager.widget.PagerAdapter;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.jajahome.feature.PagerActivity;
import com.jajahome.model.CrowDetailModel;
import com.jajahome.util.StringUtil;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by tuxuesong on 2018/5/23.
 */

public class CrowViewPagerAdapter extends PagerAdapter {


    Context mContext;
    List<CrowDetailModel.DataEntity.CrowdFundingEntity> mList;

    public CrowViewPagerAdapter(Context mContext, List<CrowDetailModel.DataEntity.CrowdFundingEntity> mList) {
        this.mContext = mContext;
        this.mList = mList;
    }

    @Override
    public int getCount() {
        if (mList == null) return 0;
        return mList.size();
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }

    @Override
    public Object instantiateItem(ViewGroup container, final int position) {
        ImageView iv = new ImageView(mContext);
        //增加占位图
        String url = mList.get(position).getImage().getUrl();
        if (!StringUtil.isEmpty(url)) {
            Picasso.with(mContext).load(url).into(iv);
        }
        iv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(mContext, PagerActivity.class);
                intent.putStringArrayListExtra(PagerActivity.DATA, getImageList());
                intent.putExtra(PagerActivity.INDEX, position);
                mContext.startActivity(intent);
            }
        });
        container.addView(iv);
        return iv;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        ImageView imageView = (ImageView) object;
        container.removeView(imageView);
    }


    public void setItemPreviewImgs(List<CrowDetailModel.DataEntity.CrowdFundingEntity> list) {
        this.mList = list;
        notifyDataSetChanged();
    }
    /**
     * 获取连接地址
     * @return ：图片地址集合
     */
    private ArrayList<String> getImageList() {
        ArrayList<String> list = new ArrayList<>();
        for (int i =0;i<mList.size();i++ ) {
            CrowDetailModel.DataEntity.CrowdFundingEntity.ImageEntity bean = mList.get(i).getImage();
                list.add(bean.getUrl());
        }
        return list;
    }
}
